package com.village.zpark.controller;

import com.github.pagehelper.PageInfo;
import com.village.zpark.common.ResultMapUtil;
import com.village.zpark.pojo.Facility;
import com.village.zpark.service.FacilityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/facility")
public class FacilityController {
    @Autowired
    private FacilityService facilityService;


    @RequestMapping
    public String login(){
        return "/facility";
    }

    @RequestMapping(value = "/facilityPage")
    public String FacilityPage(){
        return "/facilityPage";
    }

    @RequestMapping(value = "/facilityQueryById")
    public Object FacilityQueryById(@RequestParam(name = "id",required = true) Integer id, Model model){
        Facility facility = facilityService.findById(id);
        model.addAttribute("obj",facility);
        return "/facilityPage";
    }

    @RequestMapping(value = "/facilityList")
    @ResponseBody
    public Object FacilityList(){
        List<Facility> facilityList = facilityService.findAll();
        return ResultMapUtil.getHashMapList(facilityList);
    }


    @RequestMapping(value = "/addfacility")
    @ResponseBody
    public Object addfacility(Facility facility){
        try {
            facility.setBuilddate(new Date());
            facility.setMaintaindate(new Date());
            int i = facilityService.addFacility(facility);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }


    @RequestMapping(value = "/updatefacility")
    @ResponseBody
    public Object updatefacility(Facility facility){
        try {
            int i = facilityService.updateFacility(facility);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    @RequestMapping(value = "/deletefacility")
    @ResponseBody
    public Object deletefacility(Integer id){
        try {
            int i = facilityService.deleteFacilityById(id);
            return ResultMapUtil.getHashMapDel(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }


    @RequestMapping(value = "/facilityQueryPage")
    @ResponseBody
    public Map<String, Object> facilityQueryPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, String param) {
        PageInfo<Facility> facilityPageInfo= facilityService.findPage(page, limit, param);
        Map<String,Object> map=new HashMap<>();
        map.put("code", 0);
        map.put("msg", "查询数据成功");
        map.put("count", facilityPageInfo.getTotal());
        map.put("data", facilityPageInfo.getList());
        return map;
    }
}
